package
{
	import flash.display.BlendMode;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;

	public class garageSubWindow extends Sprite
	{
		private var engineX,engineY;
		
		private var rightHood:ImageSprite ;
		private var leftHood:ImageSprite ;
		private var rightBody:ImageSprite ;
		private var leftBody:ImageSprite ;
		private var rightTail:ImageSprite;
		private var leftTail:ImageSprite ;
		private var rightTop:ImageSprite ;
		private var leftTop:ImageSprite ;
		private var rightWheel:ImageSprite ;
		private var leftWheel:ImageSprite;
		private var rightHoodAcs:ImageSprite;
		private var leftHoodAcs:ImageSprite;
		
		private var colorChange:ImageSprite;
		
		
		private var background:ImageSprite = new ImageSprite();
		private var car:Car = new Car(0,0);
		private var carContainer:Sprite = new Sprite();
		
		private var close:ImageSprite = new ImageSprite();
		private var render:Render ;
		public static var wid:int = Render.SCR_WID;
		private var hgt:int = Render.SCR_HGT;
		public static var bgratio:int = wid*0.7;
		public static var xAdjust:int = 105;
		public static var yAdjust:int = 30;
		public function garageSubWindow(r:Sprite)
		{
			render = r as Render;
			init();
		}
		public function newIM(w:int,x:int,y:int,fl:String):ImageSprite{
			var i:ImageSprite = new ImageSprite();
			i.overrideSize(w,0);
			i.x=x;
			i.y=y;
			addChild(i);
			i.load(null,fl);
			return i;
		}
		public function init():void{
			carContainer.x=60;
			carContainer.y=230;
			addChild(background);
			background.overrideSize(wid,hgt);
			background.load(null,"assets/images/carparts/garageBackground.png");
			close.overrideSize(20,20);
			close.x=wid-20;
			addChild(close);
			close.load(null,"assets/images/gui/HUD_Cancel.png");
			close.addEventListener(MouseEvent.CLICK,hide);
			
			colorChange = newIM(30,wid-40,350,"assets/images/gui/HUD_ArrowR.png");
			
			rightHood = newIM(30,wid-40,50,"assets/images/gui/HUD_ArrowR.png");
			leftHood = newIM(30,wid-100,50,"assets/images/gui/HUD_ArrowL.png");
			
			rightBody = newIM(30,wid-40,100,"assets/images/gui/HUD_ArrowR.png");
			leftBody = newIM(30,wid-100,100,"assets/images/gui/HUD_ArrowL.png");
			
			rightTail = newIM(30,wid-40,150,"assets/images/gui/HUD_ArrowR.png");
			leftTail = newIM(30,wid-100,150,"assets/images/gui/HUD_ArrowL.png");
			
			rightTop = newIM(30,wid-40,200,"assets/images/gui/HUD_ArrowR.png");
			leftTop = newIM(30,wid-100,200,"assets/images/gui/HUD_ArrowL.png");

			rightWheel = newIM(30,wid-40,250,"assets/images/gui/HUD_ArrowR.png");
			leftWheel = newIM(30,wid-100,250,"assets/images/gui/HUD_ArrowL.png");
			
			rightHoodAcs = newIM(30,wid-40,300,"assets/images/gui/HUD_ArrowR.png");
			leftHoodAcs = newIM(30,wid-100,300,"assets/images/gui/HUD_ArrowL.png");
			
			addChild(carContainer);
			
			rightHood.addEventListener(MouseEvent.CLICK,hoodRight);
			leftHood.addEventListener(MouseEvent.CLICK,hoodLeft);
			leftBody.addEventListener(MouseEvent.CLICK,bodyLeft); 
			rightBody.addEventListener(MouseEvent.CLICK,bodyRight);
			rightTail.addEventListener(MouseEvent.CLICK,tailRight);
			leftTail.addEventListener(MouseEvent.CLICK,tailLeft); 
			rightTop.addEventListener(MouseEvent.CLICK,topRight);
			leftTop.addEventListener(MouseEvent.CLICK,topLeft);
			
			colorChange.addEventListener(MouseEvent.CLICK,changeColour);
			
			rightWheel.addEventListener(MouseEvent.CLICK,wheelRight);
			leftWheel.addEventListener(MouseEvent.CLICK,wheelLeft);
			
			rightHoodAcs.addEventListener(MouseEvent.CLICK,hoodAcsRight);
			leftHoodAcs.addEventListener(MouseEvent.CLICK,hoodAcsLeft);
		}
		public function hoodRight(e:Event){
			car.setHood(car.hoodID+1);
		}
		public function hoodLeft(e:Event){
			car.setHood(car.hoodID-1);
		}
		public function bodyRight(e:Event){
			car.setBody(car.bodyID+1);
		}
		public function bodyLeft(e:Event){
			car.setBody(car.bodyID-1);
		}
		public function tailRight(e:Event){
			car.setTail(car.tailID+1);
		}
		public function tailLeft(e:Event){
			car.setTail(car.tailID-1);
		}
		public function topRight(e:Event){
			car.setTop(car.topID+1);
		}
		public function topLeft(e:Event){
			car.setTop(car.topID-1);
		}
		public function wheelRight(e:Event){
			car.setWheel(car.wheelID+1);
		}
		public function wheelLeft(e:Event){
			car.setWheel(car.wheelID-1);
		}
		public function hoodAcsRight(e:Event){
			car.setHoodAcs(car.hoodAcsID+1);
		}
		public function hoodAcsLeft(e:Event){
			car.setHoodAcs(car.hoodAcsID-1);
		}
		
		public function show(x:int,y:int):void{
			if (x==-1)return;
			engineX=x;
			engineY=y;
			car = Render.engine.getCar(x,y);
			if (car==null)return;
			car.grow();
			car.shrink(550);
			trace(car.ts());
			//car.refresh();
			car.mouseEnabled=false;
			car.mouseChildren=false;
			carContainer.addChild(car);
			render.addChild(this);
		}
		public function hide(e:Event):void{
			carContainer.removeChild(car);
			render.removeChild(this);
		}
		
		public function changeColour(e:Event):void{
			car.setColour(Math.random()*0xFFFFFF);
			car.refresh();
		}
	}
}